import java.util.*;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class _128最长连续序列 {
    public int longestConsecutive(int[] nums) {
        int length = nums.length;
        if (length == 0) {
            return 0;
        }
        nums = Arrays.stream(nums).sorted().distinct().toArray();
        length = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < length; i++) {
            map.put(nums[i], i);
        }
        int n = 1;
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i < length; i++) {
            int num = nums[i - 1];
            int next = nums[i];
            if (next - num == 1) {
                n++;
            } else {
                list.add(n);
                n = 1;
            }
        }
        list.add(n);
        Integer max = Collections.max(list);
        return max;
    }

    public static void main(String[] args) {
//        int[] ints = {100, 4, 200, 1, 3, 2};
//        int[] ints = {0,3,7,2,5,8,4,6,0,1};
        int[] ints = {1,0,1,2};
        _128最长连续序列 man = new _128最长连续序列();
        System.out.println(man.longestConsecutive2(ints));

    }


    public int longestConsecutive2(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int longestStreak = 0;
        for (Integer i : set) {
            if (!set.contains(i - 1)) {
                int curNum = i;
                int current =1;
                while (set.contains(curNum + 1)) {
                    curNum++;
                    current++;
                }
                longestStreak = Math.max(longestStreak, current);
            }
        }
        return longestStreak;

    }




}
